//app.js
App({

  // 1.程序启动完毕
  onLaunch: function () {
    //调用API从本地缓存中获取数据
    var logs = wx.getStorageSync('logs') || []
    logs.unshift(Date.now())
    wx.setStorageSync('logs', logs)
    this.login();

  },

  // 2.当小程序启动，或从后台进入前台显示会触发
  onShow: function () {
    this.userlogin();
  },

  userlogin:function(){
    var that = this;
    wx.checkSession({

      fail: function () {
        that.login();
      }
    })
  },

  // 3.当小程序从前台进入后台，会触发 
  onHide: function () {
    // Do something when hide.
  },
 
  login: function () {
    var that = this
    wx.login({
      success: function (res) {
        var code = res.code;
        if (code) {
          console.log('获取用户登录凭证：' + code);

          // --------- 发送凭证 ------------------
          wx.request({
            url: that.globalData.getAccessToken,
            data: { code: code },
            header: {
              'content-type': 'application/json'
            },

            success: function (res) {

              wx.setStorageSync("AccessToken", res.data.datum.AccessToken);
              wx.setStorageSync("UserType", res.data.datum.usertype);
              wx.setStorageSync("User", res.data.datum);

              //得到用户信息
              wx.getUserInfo({
                success: function (res) {
                  that.globalData.userInfo = res.userInfo
                  typeof cb == "function" && cb(that.globalData.userInfo)
                  console.log(that.globalData.userInfo);
                }
              })
 
            }
          })
          // ------------------------------------



        } else {
          console.log('获取用户登录态失败：' + res.errMsg);
        }
      }
    });
  },

  // 5.全局数据
  globalData: {
    userInfo: null,
    contextPath: 'http://localhost:8888/ycchannel',
    getAccessToken: 'http://localhost:8888/ycchannel/app/getAccessToken.do',
    getHostpitalList: 'http://localhost:8888/ycchannel/app/hospital/getList.do',
    getCategoryList: 'http://localhost:8888/ycchannel/app/category/getListByHID.do',


    //医生
    getLevelList:'http://localhost:8888/ycchannel/app/doctor/getLevelList.do',

    //用户基本信息
    addUserInfor: 'http://localhost:8888/ycchannel/app/user/addUserInfor.do',
    getUserInfor: 'http://localhost:8888/ycchannel/app/user/getUserInfor.do',

    //病历管理
    addSufferer:'http://localhost:8888/ycchannel/app/sufferer/addSufferer.do',
    getSuffererList: 'http://localhost:8888/ycchannel/app/sufferer/getSuffererList.do',
    deleteSufferer: 'http://localhost:8888/ycchannel/app/sufferer/deleteSufferer.do',
    getSufferer: 'http://localhost:8888/ycchannel/app/sufferer/getSufferer.do',
    editSufferer: 'http://localhost:8888/ycchannel/app/sufferer/editSufferer.do',
    
    getSuffererPIC:'http://localhost:8888/ycchannel/uploadFiles/xcx/',


    //预约
    getAdditionList: 'http://localhost:8888/ycchannel/app/doctor/getAdditionList.do',
    getBCBookingList: 'http://localhost:8888/ycchannel/app/doctor/getBCBookingList.do',
    getBCBookingItem: 'http://localhost:8888/ycchannel/app/doctor/getBCBookingItem.do',
    getFreeBookingList: 'http://localhost:8888/ycchannel/app/doctor/getFreeBookingList.do',

      //在线咨询
    getDoctorList: 'http://localhost:8888/ycchannel/app/doctor/getDoctorList.do'

  }





})